//
//  ViewController.m
//  MPlayer
//
//  Created by 王俊 on 15/12/14.
//  Copyright © 2015年 王俊. All rights reserved.
//

#import "CenterViewController.h"
#import "AlbumImage.h"
#import "defines.h"
#import "masonry.h"
#import "TopView.h"
#import "SongInfo.h"
#import "BottomView.h"
#import "AFSoundItem.h"
#import "AFSoundPlayback.h"




typedef enum _AniState
{
    inital,
    running,
    rest
}AniState;

@interface CenterViewController ()

@property(nonatomic,strong) AlbumImage  *albumImageView;
@property(nonatomic,assign) AniState  aniState;
@property(nonatomic,strong) UIImageView  *backgroundImage;
@property(nonatomic,strong) UIVisualEffectView  *visualEffectView;
@property(nonatomic,strong) UIImageView  *needleImage;
@property(nonatomic,strong) TopView  *topView;
@property(nonatomic,strong) UIButton  *controlButton;
@property(nonatomic,strong) BottomView  *bottomView;
@property(nonatomic,strong) AFSoundPlayback  *player;
@property(nonatomic,strong) AFSoundItem  *item;

@end

@implementation CenterViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setUpView];

    
}

/*
  http://yinyueshiting.baidu.com/data2/music/137072432/73169351450216861320.mp3?xcode=fcec64e165f1142352d17b2d9b423fd1
 
 http://127.0.0.1/龙卷风.mp3
 */


- (void)setUpView
{
    [self preferredStatusBarStyle];// = UIStatusBarStyleLightContent;
    _aniState = inital;
    _backgroundImage = [[UIImageView alloc]init];
    _backgroundImage.image = [UIImage imageNamed:@"bgImage"];
    _backgroundImage.frame = self.view.frame;
    [self.view addSubview:_backgroundImage];

    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    _visualEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
    _visualEffectView.alpha = 1;
    _visualEffectView.frame = self.view.frame;
    [_backgroundImage addSubview:_visualEffectView];
    

    
    _albumImageView = [[AlbumImage alloc]init];
    _albumImageView.frame = CGRectMake((SCREENWIDTH - ALBUMIMAGEWIDTH)/2, (SCREENHEIGHT - ALBUMIMAGEHEIGHT)/2, ALBUMIMAGEWIDTH, ALBUMIMAGEHEIGHT);
    [_albumImageView updateRadioImage:[UIImage imageNamed:@"qq"]];
    [self.view addSubview:_albumImageView];
    
    _needleImage = [[UIImageView alloc]init];
    _needleImage.backgroundColor = [UIColor clearColor];
    _needleImage.frame = CGRectMake(SCREENWIDTH/2 - 30, _albumImageView.frame.origin.y - 100, 96, 153);
    _needleImage.image = [UIImage imageNamed:@"playNeedle"];
    //_needleImage.layer.anchorPoint = CGPointMake(0.25, 0.16);
    [self setAnchoPointWith:CGPointMake(0.25, 0.16) forView:_needleImage];
    [self.view addSubview:_needleImage];
    [UIView animateKeyframesWithDuration:0 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
        _needleImage.transform = CGAffineTransformMakeRotation(- M_PI/6);
    } completion:nil];
    
    SongInfo *info = [[SongInfo alloc]initWithSongName:@"双截棍" andSingerName:@"周杰伦"];
    _topView = [[TopView alloc]initWithSongInfo:info];
    _topView.frame = CGRectMake(0, 0, SCREENWIDTH, _needleImage.frame.origin.y + 60);
    [self.view addSubview:_topView];
    
    _controlButton = [[UIButton alloc]init];
    _controlButton.frame = _albumImageView.frame;
    _controlButton.layer.cornerRadius = _controlButton.frame.size.width/2;
    _controlButton.backgroundColor = [UIColor clearColor];
    [_controlButton addTarget:self action:@selector(controlButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_controlButton];
    
    _bottomView = [[BottomView alloc]init];
    _bottomView.frame = CGRectMake(0, SCREENHEIGHT - 50, SCREENWIDTH, 50);
    [self.view addSubview:_bottomView];
    
    [_bottomView.nextButton addTarget:self action:@selector(nextButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    NSURL *url = [NSURL URLWithString:@"http://yinyueshiting.baidu.com/data2/music/137072432/73169351450216861320.mp3?xcode=fcec64e165f1142352d17b2d9b423fd1"];
    _item = [[AFSoundItem alloc]initWithStreamingURL:url];
    
    
    
}


- (void)controlButtonClicked
{
    switch (_aniState) {
        case inital: [_albumImageView rotationAni];
            [self needleOn];
            _aniState = running;
            _player = [[AFSoundPlayback alloc]initWithItem:_item];
            //[_player play];
            
            NSLog(@"player play!");
            break;
        case running: [_albumImageView pauseAni];
            [self needleOff];
            [_player pause];
            _aniState = rest;
            break;
        case rest: [_albumImageView resumeAni];
            _aniState = running;
            [self needleOn];
            [_player play];
            break;
        default:
            break;
    }
    
}
- (void)nextButtonClicked
{
    [_player restart];
    NSLog(@"Next Button Clicked!");
}

- (void)needleOff
{
    [UIView animateKeyframesWithDuration:0.5 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
        _needleImage.transform = CGAffineTransformMakeRotation(- M_PI/6);
    } completion:nil];
    
}

- (void)needleOn
{
    [UIView animateKeyframesWithDuration:0.5 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
        _needleImage.transform = CGAffineTransformMakeRotation(0);
    } completion:nil];
    
}

- (void)setAnchoPointWith:(CGPoint) anchorPoint forView:(UIView *)view
{
    CGPoint  newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y);
    CGPoint  oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y);
    newPoint = CGPointApplyAffineTransform(newPoint, view.transform);
    oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform);
    CGPoint position = view.layer.position;
    position.x -= oldPoint.x;
    position.x += newPoint.x;
    
    position.y -= oldPoint.y;
    position.y += newPoint.y;
    
    view.layer.position = position;
    view.layer.anchorPoint = anchorPoint;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}

- (UIStatusBarStyle)preferredStatusBarStyle

{
    
    return UIStatusBarStyleLightContent;
    
}



@end
